package com.pbking.facebook.data.feed
{
	import com.adobe.serialization.json.JSON;
	
	[Bindable]
	/**
	 * template bundle.
	 * 
	 * <pre>
	 * A template bundle consists of: 
	 * 	 An array of one line story templates 
	 *   An array of short story templates 
	 *   A single full story template
	 * </pre> 
	 * 
	 * when this object is translated to JSON, 
	 * one_line-story_templates -> a JSON-encoded array of FBML templates.  
	 * short_story_templates -> a JSON-encoded array of dictionaries
	 * full_story_template -> A string that spells out a JSON-encoded dictionary with two fields: 
	 * 		template_title, which should be mapped to the FBML template used to render a full story’s title, 
	 * 		and template_body, which should map to the FBML template used to render a full story’s body.
	 * 
	 * <pre>
	 * eg.
	 * 'one_line_story_templates' -> ["{*actor*} just bought {*album*} by {*artist*} from FacebookMusic.","{*actor*}
     *                      have been listening to a lot of {*artist*} at FacebookMusic.","{*actor*} have been listening to a lot of their favorite artists at FacebookMusic."] 
	 * 'short_story_templates' -> [{"template_title":"{*actor*} just bought {*album*} by {*artist*} from FacebookMusic.","template_body":"{*actor*} just bought {*album*} by
     *                      {*artist*} from FacebookMusic, which includes {*song*}, {*song2*}, and {*song3*}."},{"template_title":"{*actor*} have been listening
     *                      to a lot of {*artist*} at FacebookMusic.","template_body":"{*actor*} have been listing to a lot of {*artist*} at FacebookMusic.  Click the image
     *                      to listen to 30 seconds of his latest single, {*song*}."}]
	 * 'full_story_template' -> {"template_title":"{*actor*} just bought {*album*} by {*artist*} from FacebookMusic.","template_body":"Click below to watch the latest {*artist*} video,
     *                   available only through FacebookMusic."}
     * </pre>
	 * 
	 * @author sol wu
	 * @see http://wiki.developers.facebook.com/index.php/Feed.registerTemplateBundle
	 */ 
	public class TemplateBundle
	{
		public var id:int;
		
		/** array of String.  each String is a one line story template.  each string must begin with {*actor*}. */
		public var one_line_story_templates:Array = new Array();
		/** an array of ShortStoryTemplate objects */
		public var short_story_templates:Array = new Array();
		/** A string that spells out a JSON-encoded dictionary with two fields: template_title, which should be mapped to the FBML template used to render a full story’s title, and template_body, which should map to the FBML template used to render a full story’s body. */
		public var full_story_template:FullStoryTemplate = null;
		
		public var time_created:Date = null;
		
		/**
		 * @param initObj - an object that is built from JSON response of Facebook API request.  
		 * 					If initObj is present, use initObj's property to populate property of this TemplateBundle.
		 * 					else, create blank object.     
		 */
		public function TemplateBundle(initObj:Object=null)
		{
			if (initObj) {
			
				this.one_line_story_templates = initObj.one_line_story_templates;
				
				if (initObj.short_story_templates) 
				{
					for each (var sst:Object in initObj.short_story_templates) 
					{
						var shortStoryTemplate:ShortStoryTemplate = new ShortStoryTemplate();
						shortStoryTemplate.template_title = sst.template_title;
						shortStoryTemplate.template_body = sst.template_body;
						
						this.short_story_templates.push(shortStoryTemplate);
					}
				}
				
				if (initObj.full_story_template)
				{
					// full_story_template's template_title and template_body are both required element.
					if (this.full_story_template == null) {
						this.full_story_template = new FullStoryTemplate();
					}
					
					this.full_story_template.template_title = initObj.full_story_template.template_title;
					this.full_story_template.template_body = initObj.full_story_template.template_body;
				}
				
				// get bundle id & created time
				this.id = initObj.bundle_template_id;
				this.time_created = new Date(initObj.time_created);
			}
		}
		
		public function get one_line_story_templates_JSON():String
		{
			return JSON.encode(one_line_story_templates);
		}
		
		public function get short_story_templates_JSON():String
		{
			return JSON.encode(short_story_templates);
		}
		
		public function get full_story_template_JSON():String
		{
			return JSON.encode(full_story_template);
		}

	}
}